package svc

import (
	"paper-pro/app/paperfile/api/internal/config"
	"paper-pro/app/paperfile/api/internal/middleware"
	"paper-pro/pkg/rpc/paperfile/paperfileclient"

	"github.com/zeromicro/go-zero/core/stores/redis"
	"github.com/zeromicro/go-zero/rest"
	"github.com/zeromicro/go-zero/zrpc"
)

type ServiceContext struct {
	Config                     config.Config
	ConcurrencyLimitMiddleware rest.Middleware
	FileCheckMiddleware        rest.Middleware
	UploadRateLimitMiddleware  rest.Middleware

	PaperfileRpc paperfileclient.PaperFile
}

func NewServiceContext(c config.Config) *ServiceContext {
	return &ServiceContext{
		Config:                     c,
		ConcurrencyLimitMiddleware: middleware.NewConcurrencyLimitMiddleware(redis.MustNewRedis(c.RedisConf), c.UploadFileConf.MaxConcurrent).Handle,
		FileCheckMiddleware:        middleware.NewFileCheckMiddleware(c.UploadFileConf.MaxFileSize).Handle,
		UploadRateLimitMiddleware:  middleware.NewUploadRateLimitMiddleware(c.UploadFileConf.BytesPerSecond).Handle,

		PaperfileRpc: paperfileclient.NewPaperFile(zrpc.MustNewClient(c.PaperfileRpcConf)),
	}
}
